package com.atguigu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.atguigu.springcloud.service.LimitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @USER: dayu
 * @DATE: 2020/4/8
 * @DESCRIPTION:
 */
@RestController
@Slf4j
public class FlowLimitController {

    @Resource
    public LimitService limitService;

    @GetMapping("/show1")
    public String testA() {
        log.info("show1");
        return "limitService.show1()";

    }

    @GetMapping("/show2")
    public String testB() {
        //休息会。。。
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "limitService.show2()";
    }

    @GetMapping("/showD")
    public String testD() {
        //休息会。。。
//        try { TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
//        log.info("testD 测试RT");

        int a = 10 / 0;
        log.info("testD,测试异常比例");
        return "limitService.testD()";
    }

    @GetMapping("/showE")
    public String testE() {
        int a = 10 / 0;
        log.info("testE,测试异常数");
        return "limitService.testE()";
    }

    @GetMapping("/jiangji")
    public String jiangji() {
//        int a = 10 / 0;
        //休息会。。。
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(Thread.currentThread().getName() + "\t " + Thread.currentThread().getId());
        return "jiangji";
    }

    @GetMapping("/hotKey")
    @SentinelResource(value = "hotKey", defaultFallback = "deal_hotKey")
    public String hotKey(@RequestParam(value = "key1", required = false) String key1,
                         @RequestParam(value = "key2", required = false) String key2) {
        int a=10/0;
        log.info("hotKey");
        return "limitService.hotKey()";
    }

    public String deal_hotKey() {
        log.info("deal_hotKey,测试异常数");
        return "deal_hotKey  \t o(╥﹏╥)o";
    }
}
